var util = require('../../libs/util.js');
var app = getApp();
Page({
  data: {
    StatusBar: app.globalData.StatusBar,
    CustomBar: app.globalData.CustomBar,
    warehouseData: [],
    warehouse: [],
    warehouseName: [],
    searchShowed: false,
    searchVal: "",
    selected: false,
    currentPage: 1,
    pageNumber: 0,
    pageRows: 20,
    firstPageDisabled: true,
    prevPageDisabled: true,
    nextPageDisabled: false,
    lastPageDisabled: false
  },
  onShareAppMessage: function () {
    return {
      title: app.appName,
      path: '/pages/index/index'
    }
  },
  onLoad: function (e) {
    wx.setNavigationBarTitle({ "title": app.appName + "--选择注册的发货点" });
    wx.removeStorage({
      key: 'path_warehouse',
      success: function (res) {
      }
    });
    if (e.users && e.users != 'undefined') {
      this.setData({
        warehouse: e.users.split(",")
      })
    }
    if (e.usersName && e.usersName != 'undefined') {
      this.setData({
        warehouseName: e.usersName.split(",")
      })
    }
    this.loadData();
  },
  onReady: function () {
    wx.hideToast()
  },
  showSearch: function () {
    this.setData({
      searchShowed: true
    });
  },
  hideSearch: function () {
    this.setData({
      searchVal: "",
      searchShowed: false
    });
    this.loadData();
  },
  clearSearch: function () {
    this.setData({
      searchVal: ""
    });
    this.loadData();
  },
  searchTyping: function (e) {
    this.setData({
      searchVal: e.detail.value
    });
    this.setData({
      currentPage: 1
    });
    this.loadData();
  },
  loadData() {
    wx.showLoading({
      title: '加载数据中...',
    });
    var that = this;
    util.getData("/atapi/user/user/list", {
      CO_ID: app.globalData.CO_ID,
      currentPage: that.data.currentPage,
      sql: that.data.searchVal,
      pageRows: that.data.pageRows
    }, function (res, error) {
      console.log(res); console.log(error)
      if (res.data.success) {
        var _data = res.data.data[0], rowNumber = Number(res.data.data[1][0].count);
        that.setData({
          pageNumber: Math.ceil(rowNumber / that.data.pageRows)
        })
        var warehouseData = [];
        var warehouse = that.data.warehouse;
        var pageSelectWarehouse = [];
        for (var i = 0; i < _data.length; i++) {
          var _item = { name: _data[i].name, value: _data[i].ID }
          if (warehouse.length > 0) {
            if (warehouse.indexOf(String(_data[i].ID)) > -1) {
              _item = { name: _data[i].name, value: _data[i].ID, checked: true };
              pageSelectWarehouse.push(String(_data[i].ID));
            }
          }
          warehouseData.push(_item)
        };
        that.setData({
          warehouseData: warehouseData,
          pageSelectWarehouse: pageSelectWarehouse
        })
        that.pageDisabled();
      }
      wx.hideLoading();
    });
  },
  loadPageData: function () {
    var that = this;
    var warehouseData = [];
    var _data = that.data.dataFilterList;
    var currentPage = that.data.currentPage;
    for (var i = 0; i < _data.length; i++) {
      var _item = { name: _data[i].name, abbreviation: _data[i].abbreviation, address: _data[i].address, value: _data[i].ID }
      if (i < currentPage * 20 && i >= (currentPage - 1) * 20) {
        warehouseData.push(_item)
      }
    };
    that.setData({
      warehouseData: warehouseData
    })
  },
  multiDataSelect: function (e) {
    var that = this;
    console.log(e.detail.value)
    var warehouseData = this.data.warehouseData, values = e.detail.value;
    //已选仓库
    var warehouse = that.data.warehouse;
    var warehouseName = that.data.warehouseName;
    
    //当页已选仓库
    var pageSelectWarehouse = that.data.pageSelectWarehouse, _pageSelectWarehouse = [];
    //从已选仓库中删除--去掉勾中时
    if (pageSelectWarehouse.length > 0) {
      for (var j = 0; j < pageSelectWarehouse.length; j++) {
        if (values.indexOf(pageSelectWarehouse[j]) > -1) {
          _pageSelectWarehouse.push(pageSelectWarehouse[j])
        } else {
          console.log('warehouse:' + warehouse)
          console.log('pageSelectWarehouse[j]:' + pageSelectWarehouse[j])
          warehouseName = util.arrayDeleteItem(warehouseName, warehouseName[warehouse.indexOf(pageSelectWarehouse[j])]);
          warehouse = util.arrayDeleteItem(warehouse, pageSelectWarehouse[j]);
        }
      }
    }
    pageSelectWarehouse = _pageSelectWarehouse;
    console.log(warehouse)
    console.log(warehouseName)
    //在已选仓库增加--勾中时
    for (var i = 0, lenI = warehouseData.length; i < lenI; ++i) {
      warehouseData[i].checked = false;
      for (var j = 0, lenJ = values.length; j < lenJ; ++j) {
        if (warehouseData[i].value == values[j]) {
          warehouseData[i].checked = true;
          if (warehouse.indexOf(String(warehouseData[i].value)) == -1) {
            warehouse.push(String(warehouseData[i].value));
            warehouseName.push(String(warehouseData[i].name));
            pageSelectWarehouse.push(String(warehouseData[i].value));
          }
          break;
        }
      }
    }

    this.setData({
      pageSelectWarehouse: pageSelectWarehouse,
      warehouseData: warehouseData,
      warehouse: warehouse,
      warehouseName: warehouseName
    });
  },
  confirmData: function () {
    var that = this;
    if (that.data.warehouse.length > 0) {
      wx.setStorage({
        key: "manager",
        data: { manager: that.data.warehouse.join(','), manager_name: that.data.warehouseName.join(',') }
      });
      wx.navigateBack({
        delta: 1
      });
    } else {
      wx.showToast({
        title: "请选择用户",
        image: "/images/wrong.svg",
        duration: 3000
      });
    }
  },
  cancelSelected: function (e) {
    wx.setStorage({
      key: "manager",
      data: { warehouse: "", warehouseName: ""}
    });
    wx.navigateBack({
      delta: 1
    });
  },
  prevPage: function () {
    var currentPage = Number(this.data.currentPage);
    if (currentPage > 1) {
      currentPage--;
    } else {
      currentPage = 1;
    }
    this.setData({
      currentPage: currentPage
    });
    this.pageDisabled();
    this.loadData();
  },
  nextPage: function () {
    var currentPage = Number(this.data.currentPage), pageNumber = this.data.pageNumber;
    if (currentPage < pageNumber) {
      currentPage++;
    } else {
      currentPage = pageNumber;
    }
    this.setData({
      currentPage: currentPage
    });
    this.pageDisabled();
    this.loadData();
  },
  firstPage: function () {
    this.setData({
      currentPage: 1
    });
    this.pageDisabled();
    this.loadData();
  },
  lastPage: function () {
    this.setData({
      currentPage: this.data.pageNumber
    });
    this.pageDisabled();
    this.loadData();
  },
  pageDisabled: function () {
    var currentPage = Number(this.data.currentPage), pageNumber = this.data.pageNumber;
    if (pageNumber == 1) {
      this.setData({
        firstPageDisabled: true,
        prevPageDisabled: true,
        nextPageDisabled: true,
        lastPageDisabled: true
      });
    } else if (currentPage == 1) {
      this.setData({
        firstPageDisabled: true,
        prevPageDisabled: true,
        nextPageDisabled: false,
        lastPageDisabled: false
      });
    } else if (currentPage < pageNumber) {
      this.setData({
        firstPageDisabled: false,
        prevPageDisabled: false,
        nextPageDisabled: false,
        lastPageDisabled: false
      });
    } else if (currentPage == pageNumber) {
      this.setData({
        firstPageDisabled: false,
        prevPageDisabled: false,
        nextPageDisabled: true,
        lastPageDisabled: true
      });
    }
  },
  cancelConfirmData: function () {
    wx.navigateBack({
      delta: 1
    });
  }
});